#include "track.h"

#include <QFileInfo>

// TagLib
#include <tag.h>
#include <fileref.h>

Track::Track()
{

}

void Track::setPath(const QString &trackPath)
{
    path = trackPath;
    dir = QFileInfo(trackPath).absolutePath();

    // tags
    TagLib::FileRef trackFile(qPrintable(trackPath));

    title = trackFile.tag()->title().to8Bit().c_str();
    artist = trackFile.tag()->artist().to8Bit().c_str();
    album = trackFile.tag()->album().to8Bit().c_str();
    year = trackFile.tag()->year();

    // cover    
    QString coverPath = dir + "/cover.jpg";
    QPixmap pixmap(coverPath);
    if (!pixmap.isNull()) {
        cover = pixmap.scaled(150, 150, Qt::KeepAspectRatio);
    } else {
        cover = QPixmap(":/images/NoData");
    }

    // useful infos
    length = trackFile.audioProperties()->length();

    emit trackChanged(this);
}

QString Track::getPath()
{
    return path;
}

QString Track::getTitle()
{
    return title;
}
QString Track::getArtist()
{
    return artist;
}
QString Track::getAlbum()
{
    return album;
}

int Track::getYear()
{
    return year;
}

QPixmap Track::getCover()
{
    return cover;
}

int Track::getLength()
{
    return length;
}
